package net.hekka.canyoutalk.data.executor;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import net.hekka.canyoutalk.data.store.NumberTableSqlStore;
import net.hekka.canyoutalk.share.database.SqlExecutor;

/**
 * numbertable 테이블에 관련된 SQL 실행에 대한 클래스이다.
 * @author Jeffrey Ha
 */
public class NumberTableSqlExecutor extends SqlExecutor {

    /**
     * numbertable 테이블에 새로운 컨텐츠를 insert한다.
     * @param dbConnection 데이터베이스 Connection
     * @param owner 본인 전화번호
     * @param friend 지인 전화번호
     * @return 입력된 갯수
     * @throws SQLException SQL 작업중 오류 발생 시
     */
    public int insertNumberTable(Connection dbConnection, String owner, String friend) throws SQLException {
        int count = 0;
        try {
            //set Database connection
            setDbConnection(dbConnection);
            //set DatabaseType
            setDatabaseType();
            //get sql statement
            String sql = NumberTableSqlStore.getNumberTableInsertSql(getDatabaseType());
            //set preparedstatement
            setPreparedStatement(sql);
            //set parameter of statement
            setContentStageInsertParameter(owner, friend);
            //execute Sql
            count = getPreparedStatement().executeUpdate();
        } finally {
            try {
                if (getPreparedStatement() != null) {
                    getPreparedStatement().close();
                }
            } catch (Exception ex) {
            }
        }
        return count;
    }

    private void setContentStageInsertParameter(String owner, String friend) throws SQLException {
        int sequence = 1;

        // 콘텐츠 아이디
        this.setStringPreparedStatement(sequence++, owner);
        // 주도권자 아이디
        this.setStringPreparedStatement(sequence++, friend);
    }

    /**
     * NumberTable에서 조건에 맞는 데이터수를 구한다.
     * @param dbConnection 데이터베이스 Connection
     * @return NumberTable에서 조건에 맞는 데이터수
     * @throws SQLException SQL문 실행 중 오류 발생 시
     */
    public int getCountOfNumberTable( Connection dbConnection, String owner, String friend ) throws SQLException {
        ResultSet rs = null;
        int totalCount = 0;
        try {
            // set Database Connection
            setDbConnection(dbConnection);
            // set Database Type
            setDatabaseType();
            // get sql statement
            String sql = NumberTableSqlStore.getCountOfNumberTableSelectSql(getDatabaseType());
            // set PrepareStatement
            setPreparedStatement(sql);
            // set parameters of Preparestatement
            this.setStringPreparedStatement(1, owner);
            this.setStringPreparedStatement(2, friend);

            // execute sql
            rs = getPreparedStatement().executeQuery();
            while (rs.next()) {
                totalCount = rs.getInt("count");
            }

        } finally {
            try {
                if (getPreparedStatement() != null) {
                    getPreparedStatement().close();
                }
            } catch (Exception ex2) {
            }
        }
        return totalCount;
    }



}
